﻿using System;
using System.Text;
using Unity.Type.BinaryType;

namespace Unity.Type.Examples
{
    class Program
    {
        private static Metadata PrepareTestMetadataObject()
        {
            Metadata metadata = new Metadata();
            metadata.UnityId = new ReadOnlySpan<byte>(Encoding.ASCII.GetBytes("144347:256234"));
            metadata.Version = 123;
            metadata.ChangerCsiId = 144347;
            metadata.Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
            metadata.EntityType = EntityType.Order;
            metadata.EventType = EventType.Update;
            metadata.FormatVer = 1;
            metadata.VersionDelete = 456;
            return metadata;
        }

        static void Main(string[] args)
        {
            //string unityId = "123456";
            //byte[] result = UnityIdToBinary(unityId);
            //Console.WriteLine(BitConverter.ToString(result));

            Metadata testMetadata = PrepareTestMetadataObject();
            MetadataSerializer testMetadataSerializer = new MetadataSerializer();
            byte[] serialMetadata = testMetadataSerializer.Serialize(testMetadata);
           

            MetadataDeserializer testMedataDeserializer = new MetadataDeserializer();
            Metadata deserialMetadata = testMedataDeserializer.Deserialize(serialMetadata);
            if(deserialMetadata == null)
            {
                Console.WriteLine("deserial is null");
            }
            Console.WriteLine(deserialMetadata);
        }

        private static byte[] UnityIdToBinary(string value)
        {
            int length = Metadata.UnityIdLength;
            if (value.Length > length)
            {
                throw new IndexOutOfRangeException("value size more than unityId limit");
            }
            byte[] binary = new byte[length];

            int offset = length - value.Length;
            for (int i = 0; i < length; i++)
            {
                if (i < offset)
                {
                    binary[i] = (byte)'0';
                }
                else
                {
                    binary[i] = (byte)value[i - offset];
                }
            }

            return binary;
        }
    }
}
